package com.edu.koudingmusic.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.edu.koudingmusic.R;
import com.edu.koudingmusic.entities.SearchResult;

import java.util.List;

/**
 * Created by Ming on 2016/2/27.
 * email 1715944993@qq.com
 */
public class NetMusicAdapter extends BaseAdapter {

    private Context mContext;
    private List<SearchResult> mSearchResults;

    public NetMusicAdapter(Context context, List<SearchResult> searchResults) {
        mContext = context;
        mSearchResults = searchResults;
    }


    public List<SearchResult> getSearchResults() {
        return mSearchResults;
    }

    public void setSearchResults(List<SearchResult> searchResults) {
        mSearchResults = searchResults;
    }

    @Override
    public int getCount() {
        return mSearchResults.size();
    }

    @Override
    public Object getItem(int position) {
        return mSearchResults.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.net_item_music_list, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        SearchResult searchResult = mSearchResults.get(position);
        viewHolder.mNetMusicTitle.setText(searchResult.getMusicName());
        viewHolder.mNetMusicSinger.setText(searchResult.getArtist());

        return convertView;
    }


    static class ViewHolder {

        private TextView mNetMusicSinger;
        private TextView mNetMusicTitle;

        public ViewHolder(View itemView) {
            mNetMusicTitle = (TextView) itemView.findViewById(R.id.net_item_music_title);
            mNetMusicSinger = (TextView) itemView.findViewById(R.id.net_item_music_singer);

        }
    }
}
